package com.mice.microserviceconsumer.controller;

import com.mice.microserviceconsumer.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by AmbitiousMice on 2018/9/12.
 */
@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/user/{id}")
    public User findById(@PathVariable Integer id){

        return restTemplate.getForObject("http://127.0.0.1:8080/user/"+id,User.class);
    }

    /**
     * 查询microservice-provider-user服务的信息返回
     * @return
     */
    @GetMapping("/user-instance")
    public List<ServiceInstance> showInfo(){
        return this.discoveryClient.getInstances("microservice-provider-user-my-metadata");
    }
}
